package com.example.demojava.controller;

import com.example.demojava.entity.Sample;
import com.example.demojava.service.SampleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 样本管理接口
 */
@RestController
@RequestMapping("/samples")
public class SampleController {

    @Autowired
    private SampleService sampleService;

    /**
     * 创建样本
     * 
     * 请求示例：
     * POST /api/samples
     * Content-Type: application/json
     * 
     * {
     * "name": "测试样本",
     * "code": "SAMPLE001",
     * "fileName": "test.pdf"
     * }
     */
    @PostMapping
    public ResponseEntity<Sample> createSample(@RequestBody Sample sample) {
        return ResponseEntity.ok(sampleService.createSample(sample));
    }

    /**
     * 获取样本列表（分页）
     * 
     * 请求示例：
     * GET /api/samples?page=0&size=10&sort=createdAt,desc
     */
    @GetMapping
    public ResponseEntity<Page<Sample>> findAll(Pageable pageable) {
        return ResponseEntity.ok(sampleService.findAll(pageable));
    }

    /**
     * 获取单个样本
     * 
     * 请求示例：
     * GET /api/samples/1
     */
    @GetMapping("/{id}")
    public ResponseEntity<Sample> findById(@PathVariable Long id) {
        return ResponseEntity.ok(sampleService.findById(id));
    }

    /**
     * 更新样本
     * 
     * 请求示例：
     * PUT /api/samples/1
     * Content-Type: application/json
     * 
     * {
     * "name": "更新后的样本名称",
     * "code": "SAMPLE001",
     * "fileName": "updated.pdf"
     * }
     */
    @PutMapping("/{id}")
    public ResponseEntity<Sample> updateSample(@PathVariable Long id, @RequestBody Sample sample) {
        return ResponseEntity.ok(sampleService.updateSample(id, sample));
    }

    /**
     * 删除样本
     * 
     * 请求示例：
     * DELETE /api/samples/1
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteSample(@PathVariable Long id) {
        sampleService.deleteSample(id);
        return ResponseEntity.ok().build();
    }
}